import sys
from PySide6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
                               QTabWidget, QDockWidget, QTableWidget, QTextEdit, QLabel,
                               QLineEdit, QPushButton, QSplitter, QMenuBar, QToolBar,
                               QStatusBar, QFrame, QGroupBox)
from PySide6.QtCore import Qt, QSize
import pyqtgraph as pg
from pyqtgraph import PlotWidget


class PyramidTradingUI(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("金字塔决策交易系统")
        self.setGeometry(100, 100, 1400, 900)

        # 设置中文字体支持
        #pg.setConfigOption('font', {'family': ['SimHei', 'WenQuanYi Micro Hei', 'Heiti TC']})

        # 创建中心部件
        self.central_widget = QTabWidget()
        self.setCentralWidget(self.central_widget)

        # 创建菜单栏和工具栏
        self.create_menu_bar()
        self.create_tool_bar()

        # 创建停靠窗口
        self.create_dock_windows()

        # 创建状态栏
        self.statusBar = QStatusBar()
        self.setStatusBar(self.statusBar)
        self.statusBar.showMessage("就绪")

        # 添加中央K线图标签页
        self.add_chart_tab()

    def create_menu_bar(self):
        """创建菜单栏"""
        menubar = self.menuBar()

        # 文件菜单
        file_menu = menubar.addMenu('文件')
        file_menu.addAction('新建策略')
        file_menu.addAction('打开策略')
        file_menu.addAction('保存策略')
        file_menu.addSeparator()
        file_menu.addAction('退出')

        # 视图菜单
        view_menu = menubar.addMenu('视图')
        view_menu.addAction('重置布局')
        view_menu.addAction('全屏显示')

        # 策略菜单
        strategy_menu = menubar.addMenu('策略')
        strategy_menu.addAction('编译策略')
        strategy_menu.addAction('回测')
        strategy_menu.addAction('实盘运行')

        # 帮助菜单
        help_menu = menubar.addMenu('帮助')
        help_menu.addAction('使用手册')
        help_menu.addAction('关于')

    def create_tool_bar(self):
        """创建工具栏"""
        toolbar = QToolBar('主工具栏')
        toolbar.setIconSize(QSize(16, 16))
        self.addToolBar(toolbar)

        toolbar.addAction('刷新行情')
        toolbar.addAction('启动所有策略')
        toolbar.addAction('暂停所有策略')
        toolbar.addSeparator()
        toolbar.addAction('连接行情')
        toolbar.addAction('断开连接')

    def create_dock_windows(self):
        """创建所有停靠窗口"""
        # 左侧：行情报价停靠窗
        self.create_quote_dock()

        # 右侧：交易和持仓停靠窗
        self.create_trading_dock()
        self.create_position_dock()

        # 底部：日志停靠窗
        self.create_log_dock()

        # 右侧：策略管理停靠窗
        self.create_strategy_dock()

    def create_quote_dock(self):
        """创建行情报价停靠窗"""
        dock = QDockWidget("行情报价")
        dock.setAllowedAreas(Qt.DockWidgetArea.LeftDockWidgetArea | Qt.DockWidgetArea.RightDockWidgetArea)

        # 创建行情表格
        quote_widget = QWidget()
        quote_layout = QVBoxLayout(quote_widget)

        # 行情标签页
        quote_tabs = QTabWidget()

        # 国内期货行情表
        future_table = QTableWidget()
        future_table.setColumnCount(5)
        future_table.setHorizontalHeaderLabels(["合约", "最新价", "涨跌", "成交量", "持仓量"])
        quote_tabs.addTab(future_table, "国内期货")

        # 股票行情表
        stock_table = QTableWidget()
        stock_table.setColumnCount(5)
        stock_table.setHorizontalHeaderLabels(["代码", "名称", "最新价", "涨跌幅", "成交量"])
        quote_tabs.addTab(stock_table, "股票")

        quote_layout.addWidget(quote_tabs)
        dock.setWidget(quote_widget)
        self.addDockWidget(Qt.DockWidgetArea.LeftDockWidgetArea, dock)

    def create_trading_dock(self):
        """创建交易下单停靠窗"""
        dock = QDockWidget("快速下单")
        dock.setAllowedAreas(Qt.DockWidgetArea.RightDockWidgetArea)

        trading_widget = QWidget()
        trading_layout = QVBoxLayout(trading_widget)

        # 合约选择
        trading_layout.addWidget(QLabel("合约代码:"))
        trading_layout.addWidget(QLineEdit())

        # 价格和数量
        price_layout = QHBoxLayout()
        price_layout.addWidget(QLabel("价格:"))
        price_layout.addWidget(QLineEdit())
        trading_layout.addLayout(price_layout)

        volume_layout = QHBoxLayout()
        volume_layout.addWidget(QLabel("数量:"))
        volume_layout.addWidget(QLineEdit())
        trading_layout.addLayout(volume_layout)

        # 买卖按钮
        button_layout = QHBoxLayout()
        button_layout.addWidget(QPushButton("买入"))
        button_layout.addWidget(QPushButton("卖出"))
        trading_layout.addLayout(button_layout)

        # 分隔线
        line = QFrame()
        line.setFrameShape(QFrame.HLine)
        line.setFrameShadow(QFrame.Sunken)
        trading_layout.addWidget(line)

        # 高级下单选项
        advanced_group = QGroupBox("高级选项")
        advanced_layout = QVBoxLayout()
        advanced_layout.addWidget(QLabel("下单类型: 限价单"))
        advanced_layout.addWidget(QLabel("有效期: 当日有效"))
        advanced_group.setLayout(advanced_layout)
        trading_layout.addWidget(advanced_group)

        dock.setWidget(trading_widget)
        self.addDockWidget(Qt.RightDockWidgetArea, dock)

    def create_position_dock(self):
        """创建持仓列表停靠窗"""
        dock = QDockWidget("持仓列表")
        dock.setAllowedAreas(Qt.RightDockWidgetArea)

        position_widget = QWidget()
        position_layout = QVBoxLayout(position_widget)

        position_table = QTableWidget()
        position_table.setColumnCount(6)
        position_table.setHorizontalHeaderLabels(["合约", "方向", "持仓量", "均价", "浮盈", "可用资金"])
        position_layout.addWidget(position_table)

        dock.setWidget(position_widget)
        self.tabifyDockWidget(self.findChild(QDockWidget, "快速下单"), dock)
        dock.raise_()

    def create_log_dock(self):
        """创建日志停靠窗"""
        dock = QDockWidget("系统日志")
        dock.setAllowedAreas(Qt.BottomDockWidgetArea)

        log_widget = QWidget()
        log_layout = QVBoxLayout(log_widget)

        log_tabs = QTabWidget()

        # 系统日志
        self.system_log = QTextEdit()
        self.system_log.setReadOnly(True)
        log_tabs.addTab(self.system_log, "系统日志")

        # 策略日志
        self.strategy_log = QTextEdit()
        self.strategy_log.setReadOnly(True)
        log_tabs.addTab(self.strategy_log, "策略日志")

        log_layout.addWidget(log_tabs)
        dock.setWidget(log_widget)
        self.addDockWidget(Qt.BottomDockWidgetArea, dock)

    def create_strategy_dock(self):
        """创建策略管理停靠窗"""
        dock = QDockWidget("策略管理")
        dock.setAllowedAreas(Qt.RightDockWidgetArea)

        strategy_widget = QWidget()
        strategy_layout = QVBoxLayout(strategy_widget)

        # 策略列表
        strategy_table = QTableWidget()
        strategy_table.setColumnCount(4)
        strategy_table.setHorizontalHeaderLabels(["策略名称", "状态", "累计收益", "操作"])
        strategy_layout.addWidget(strategy_table)

        # 策略控制按钮
        ctrl_layout = QHBoxLayout()
        ctrl_layout.addWidget(QPushButton("添加策略"))
        ctrl_layout.addWidget(QPushButton("编辑策略"))
        ctrl_layout.addWidget(QPushButton("删除策略"))
        strategy_layout.addLayout(ctrl_layout)

        dock.setWidget(strategy_widget)
        self.addDockWidget(Qt.RightDockWidgetArea, dock)

    def add_chart_tab(self):
        """添加K线图标签页"""
        chart_widget = QWidget()
        chart_layout = QVBoxLayout(chart_widget)

        # K线图工具栏
        chart_toolbar = QToolBar('图表工具栏')
        chart_toolbar.addAction('1分钟')
        chart_toolbar.addAction('5分钟')
        chart_toolbar.addAction('15分钟')
        chart_toolbar.addAction('1小时')
        chart_toolbar.addAction('日线')
        chart_toolbar.addSeparator()
        chart_toolbar.addAction('MACD')
        chart_toolbar.addAction('KDJ')
        chart_toolbar.addAction('RSI')
        chart_layout.addWidget(chart_toolbar)

        # K线图
        self.kline_plot = PlotWidget()
        self.kline_plot.setTitle("合约K线图")
        chart_layout.addWidget(self.kline_plot)

        self.central_widget.addTab(chart_widget, "技术分析")

        # 添加其他标签页
        self.central_widget.addTab(QWidget(), "资金曲线")
        self.central_widget.addTab(QWidget(), "订单流水")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    # 设置应用样式
    app.setStyle('Fusion')
    window = PyramidTradingUI()
    window.show()
    sys.exit(app.exec())